package com.ruoyi.system.service.impl;

import com.ruoyi.common.enums.ViewRelationMetadataEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.FactoryCarAccount;
import com.ruoyi.system.domain.SecurityValveAccount;
import com.ruoyi.system.domain.ViewRelationMetadata;
import com.ruoyi.system.mapper.FactoryCarAccountMapper;
import com.ruoyi.system.service.FactoryCarAccountService;
import com.ruoyi.system.service.ViewRelationMetadataService;
import org.apache.commons.collections.MapUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 厂内车辆台账(FactoryCarAccount)表服务实现类
 *
 * @author makejava
 * @since 2022-12-14 12:00:10
 */
@Service("factoryCarAccountService")
public class FactoryCarAccountServiceImpl extends ViewRelationMetadataAbstract<List<FactoryCarAccount>> implements FactoryCarAccountService {
    @Resource
    private ViewRelationMetadataService viewRelationMetadataService;
    @Resource
    private FactoryCarAccountMapper factoryCarAccountDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public FactoryCarAccount queryById(Integer id) {
        return this.factoryCarAccountDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param factoryCarAccount 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<FactoryCarAccount> queryByPage(FactoryCarAccount factoryCarAccount, PageRequest pageRequest) {
        long total = this.factoryCarAccountDao.count(factoryCarAccount);
        return new PageImpl<>(this.factoryCarAccountDao.queryAllByLimit(factoryCarAccount, pageRequest), pageRequest, total);
    }

    @Override
    public List<FactoryCarAccount> queryAll(FactoryCarAccount factoryCarAccount) {
        List<FactoryCarAccount> factoryCarAccounts = factoryCarAccountDao.queryAll(factoryCarAccount);
        return convert(factoryCarAccounts);
    }

    /**
     * 新增数据
     *
     * @param factoryCarAccount 实例对象
     * @return 实例对象
     */
    @Override
    public FactoryCarAccount insert(FactoryCarAccount factoryCarAccount) {
        this.factoryCarAccountDao.insert(factoryCarAccount);
        return factoryCarAccount;
    }

    /**
     * 修改数据
     *
     * @param factoryCarAccount 实例对象
     * @return 实例对象
     */
    @Override
    public FactoryCarAccount update(FactoryCarAccount factoryCarAccount) {
        this.factoryCarAccountDao.update(factoryCarAccount);
        return this.queryById(factoryCarAccount.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.factoryCarAccountDao.deleteById(id) > 0;
    }

    @Override
    public String importExcel(List<FactoryCarAccount> factoryCarAccounts, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(factoryCarAccounts) || factoryCarAccounts.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        factoryCarAccountDao.insertBatch(factoryCarAccounts);
        return "成功";
    }

    @Override
    public List<FactoryCarAccount> convert(List<FactoryCarAccount> factoryCarAccounts) {
        if (!CollectionUtils.isEmpty(factoryCarAccounts)) {
            List<Integer> ids = factoryCarAccounts.stream().map(FactoryCarAccount::getId).collect(Collectors.toList());
            Map<String, ViewRelationMetadata> relationMetadataMap = viewRelationMetadataService.queryMappingRelation(ViewRelationMetadataEnum.FACTORY_CAR_ACCOUNT.name(), ids);
            if (MapUtils.isNotEmpty(relationMetadataMap)) {
                for (FactoryCarAccount values : factoryCarAccounts) {
                    String key = ViewRelationMetadataEnum.FACTORY_CAR_ACCOUNT.name() + values.getId().toString();
                    ViewRelationMetadata viewRelationMetadata = relationMetadataMap.get(key);
                    if (null != viewRelationMetadata) {
                        values.setMetadataFileName(viewRelationMetadata.getMetadataFileName());
                        values.setFileId(viewRelationMetadata.getFileId());
                        values.setFileName(viewRelationMetadata.getFileName());
                        values.setFileUrl(viewRelationMetadata.getFileUrl());
                    }
                }
            }
        }
        return factoryCarAccounts;
    }
}
